package com.main.controller;

import com.google.gson.Gson;
import com.main.common.Person;
import com.main.common.Response;
import com.main.entity.User;
import com.main.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


@RestController
public class TestController {
    @Resource
    private UserService userService;

    @GetMapping("/query")
    public void streamData(HttpServletResponse response) throws IOException {
        // 创建一个较大的Person对象数组
        List<Person> peopleList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) { // 增加更多对象
            peopleList.add(new Person("Person" + i, i % 100));
        }
        Person[] people = peopleList.toArray(new Person[0]);
        // 创建一个通用的响应对象
        Response<Person[]> success = Response.success(people);
        // 使用Gson将数组转换为JSON字符串
        Gson gson = new Gson();
        String json = gson.toJson(success);

        // 将JSON字符串转换为字节数组
        byte[] data = json.getBytes();
        int length = data.length;



        // 设置响应头
//        response.setContentType("application/octet-stream");
//        response.setContentLength(length);
        // 设置响应头
        response.setContentType("application/json"); // 更改为正确的Content-Type
        response.setCharacterEncoding("UTF-8"); // 设置字符编码
        response.setStatus(HttpServletResponse.SC_OK); // 设置HTTP状态码
        response.setContentLength(length);

        // 获取输出流
        OutputStream out = response.getOutputStream();

        // 模拟数据传输过程中的进度报告
        for (int i = 0; i < length; i++) {
            out.write(data[i]);
            // 这里可以触发一些逻辑来模拟进度报告
            if (i % 100 == 0 && i > 0) {
                System.out.println("Sent " + (i * 100 / length) + "% of the data.");
            }
        }

        // 完成写入，关闭输出流
        out.flush();
        out.close();
    }
    @GetMapping("/addData")
    public void addData() {
        long start = System.currentTimeMillis();
        List<User> list = new ArrayList<>(1000);
        for (int i = 0; i < 1000000; i++) {
            list.add(new User("name" + i, "address" + i));
            if (list.size() == 1000) {
                userService.saveBatch(list);
                list.clear();
            }
        }
//        if (!list.isEmpty()) {
//            userService.saveBatch(list); // 插入剩余的数据
//        }
        long end = System.currentTimeMillis();
        long durationMillis = end - start;
        long durationSeconds = durationMillis / 1000;
        long durationMinutes = durationSeconds / 60;
        durationSeconds %= 60;
        System.out.println("插入100万数据:"+durationMillis+"ms");
        System.out.println("插入100万数据耗时：" + durationMinutes + "分钟 " + durationSeconds + "秒 " + (durationMillis % 1000) + "毫秒");

    }
}
